package com.example.homework7;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.material.navigation.NavigationView;

import fragments.MainFragment;
import fragments.SqliteFragment;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private Intent intent;
    private Fragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
    }

    private void initView() {
        toolbar = findViewById(R.id.toolbar);
        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
    }

    private void initEvent() {
        setBar();
        setDrawerLayoutNavigationView();
    }

    /**
     * 配置抽屉导航栏
     */
    private void setDrawerLayoutNavigationView() {
        //设置默认选中的子项
        navigationView.setCheckedItem(R.id.nav_home_page);
        //设置抽屉导航栏每个item的点击事件
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //每个item的执行逻辑
                switch (item.getItemId()){
                    case R.id.nav_home_page://导航栏的首页item
                        Log.d(TAG, "onNavigationItemSelected: 点击了导航栏的item: 首页");
                        fragment = new MainFragment();
                        replaceFragment(fragment);
                        drawerLayout.closeDrawers();//关闭抽屉
                        break;
                    case R.id.nav_storageInternal://导航栏的内部存储item
                        Log.d(TAG, "onNavigationItemSelected: 点击了导航栏的item: 内部存储");
                        fragment = StorageInternalFragment.newInstance();
                        replaceFragment(fragment);
                        drawerLayout.closeDrawers(); //关闭抽屉
                        break;
                    case R.id.nav_storageExternal://导航栏的外部存储item
                        Log.d(TAG, "onNavigationItemSelected: 点击了导航栏的item: 外部存储");
                        fragment = StorageExternalFragment.newInstance();
                        replaceFragment(fragment);
                        drawerLayout.closeDrawers(); //关闭抽屉
                        break;
                    case R.id.nav_storageSharedPreferences://导航栏的SharedPreference
                        Log.d(TAG, "onNavigationItemSelected: 点击了导航栏的item: SharedPreference ");
                        intent = new Intent(MainActivity.this, SharedPreferencesActivity.class);
                        startActivity(intent);
                        drawerLayout.closeDrawers(); //关闭抽屉
                        break;

                    case R.id.nav_sqLite://导航栏的sqLite基本操作item
                        Log.d(TAG, "onNavigationItemSelected: 点击了导航栏的item: 基本操作 ");
                        fragment = SqliteFragment.newInstance();
                        replaceFragment(fragment);
                        drawerLayout.closeDrawers(); //关闭抽屉
                        break;
                    case R.id.nav_room://导航栏的room库item
                        Log.d(TAG, "onNavigationItemSelected: 点击了导航栏的item: room库 ");
                        fragment = RoomFragment.newInstance();
                        replaceFragment(fragment);
                        drawerLayout.closeDrawers(); //关闭抽屉
                        break;
                }
                return true;
            }
        });
    }

    /**
     * 配置标题栏
     */
    private void setBar() {
//        setSupportActionBar(toolbar);
       setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.icon_menu);
            actionBar.setTitle("首页");
        }
    }

    /**
     * 初始化标题栏右侧菜单栏
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    /**
     * 设置标题栏右侧菜单栏中每个item的点击事件
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_home_page://首页
                fragment = new MainFragment();
                replaceFragment(fragment);
                break;
            case R.id.storageInternal://内部存储
                fragment = StorageInternalFragment.newInstance();
                replaceFragment(fragment);
                break;
            case R.id.storageExternal://外部存储
                fragment = StorageExternalFragment.newInstance();
                replaceFragment(fragment);
                break;
            case R.id.storageSharedPreferences://SharedPreferences
                Log.d(TAG, "onOptionsItemSelected: 跳转到登录页面(SharedPreferencesActivity)");
                intent = new Intent(MainActivity.this, SharedPreferencesActivity.class);
                startActivity(intent);
                break;
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
        }
        return true;
    }

    /**
     * 动态切换fragment
     * @param fragment
     */
    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_main, fragment);
//  transaction.addToBackStack(null); //将fragment添加到专门的栈里进行统一管理
        transaction.commit();
    }
}